//
//  IGMyLikeController.m
//  jingdongfang
//
//  Created by MTiMac02 on 2018/5/10.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGMyLikeController.h"
#import "HomeViewController.h"
#import "MyLikeCategoryCell.h"
#import "iGGetCategoryApi.h"
#import "iGSetCategoryApi.h"
#import "GCategoryListModel.h"

@interface IGMyLikeController ()<UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    UIView  *headerView;
    UIButton *nextStep;
    NSMutableArray *selArr;
    NSInteger selCount;
    GCategoryListModel *info;

}
@property(nonatomic,strong)iGGetCategoryApi *getCategoryApi;
@property(nonatomic,strong)UICollectionView *categoryView;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, copy) NSString *queryTime;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end

@implementation IGMyLikeController
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr=[NSMutableArray array];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setheadView];
    [self setcollectview];
    [self setnextStep];
    self.getCategoryApi=[iGGetCategoryApi new];
    [self firstPage];
}
- (void)firstPage
{
    _pageSize = 12;
    _pageNumber = 0;
    _queryTime = @"";
    [self requestDate];
}
-(void)requestDate{
    self.getCategoryApi.pageNumber=_pageNumber;
    self.getCategoryApi.pageSize=_pageSize;
    self.getCategoryApi.queryTime=_queryTime;
    @KWeakObj(self)
    [self.getCategoryApi startWithDecodeClass:[iGGetCategoryResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
                iGGetCategoryResp *resp=responseObject;
                iGGetCategoryModel *model=resp.data;
            if (model.lstLabelSettings&&model.lstLabelSettings.count>0) {
                [selfWeak setupFooter];
            } else
            {
                [selfWeak.categoryView ig_endFooterRefreshingWithNoMoreData];
            }
           selfWeak.queryTime = model.time;
            if (_pageNumber==0)
            {
                _dataArr = [NSMutableArray arrayWithArray:model.lstLabelSettings];
            }
            else
            {
                [_dataArr addObjectsFromArray:model.lstLabelSettings];
               
            }
            for (int i=0; i<model.lstLabelSettings.count; i++) {
                [selArr addObject:@(NO)];
            }
            [self.categoryView reloadData];
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.categoryView ig_setFooterWithRefreshingBlock:^{
        selfWeak.getCategoryApi.queryTime =_queryTime;
        [selfWeak requestDate];
    }];
}
-(void)setheadView{
    headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 112)];
    headerView.backgroundColor=[UIColor clearColor];
    UILabel *foldLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 64, KSCREENWIDTH, 18)];
    foldLabel.text=@"选择你更喜欢的,让我更懂你";
    foldLabel.backgroundColor=[UIColor clearColor];
    foldLabel.font=[UIFont boldSystemFontOfSize:20];
    foldLabel.textColor=[UIColor c4Color];
    foldLabel.textAlignment=NSTextAlignmentCenter;
    foldLabel.centerX=KSCREENWIDTH/2;
    [headerView addSubview:foldLabel];
    
    UILabel *reLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, foldLabel.bottom+15, KSCREENWIDTH, 14)];
    reLabel.text=@"我们将努力推荐您感兴趣的内容";
    reLabel.font=[UIFont font14];
    reLabel.backgroundColor=[UIColor clearColor];
    reLabel.textColor=[UIColor c5Color];
    reLabel.textAlignment=NSTextAlignmentCenter;
    reLabel.centerX=KSCREENWIDTH/2;
    [headerView addSubview:reLabel];
    [self.view addSubview:headerView];
}
-(void)setcollectview{
    self.categoryView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
     self.categoryView.backgroundColor=[UIColor c3Color];
    self.categoryView.delegate = self;
    self.categoryView.showsVerticalScrollIndicator=NO;
    self.categoryView.dataSource = self;
    [self.categoryView registerNib:[UINib nibWithNibName:@"MyLikeCategoryCell" bundle:nil] forCellWithReuseIdentifier:@"MyLikeCategoryCell"];
    [self.view addSubview:self.categoryView];
    [self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(44);
        make.right.mas_offset(-44);
        make.bottom.mas_offset(-39-44);
        make.top.mas_offset(112 + 15);
    }];
}
-(void)setnextStep{
    nextStep=[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.layer.cornerRadius=20;
    nextStep.layer.masksToBounds=YES;
    nextStep.backgroundColor=[UIColor c1Color];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStep setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextStep.titleLabel setTextAlignment:NSTextAlignmentCenter];
    nextStep.titleLabel.font=[UIFont font18];
    [nextStep addTarget:self action:@selector(nextStepClick:) forControlEvents:UIControlEventTouchUpInside];;
    [self.view addSubview:nextStep];
    [nextStep mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(75.0/2);
        make.right.mas_equalTo(-75.0/2);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(-39);
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = YES;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}
#pragma mark---UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.dataArr.count ? :  0;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyLikeCategoryCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"MyLikeCategoryCell" forIndexPath:indexPath];
    cell.data=_dataArr[indexPath.item];
    cell.isSelected = [selArr[indexPath.item]boolValue] ;
    [cell setNeedsLayout];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyLikeCategoryCell *cell = (MyLikeCategoryCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    if(selArr.count == 0){
        //如果选中的数组为空，则直接添加话题到数组中
    }else{
        
        for (int i=0; i<selArr.count; i++) {
            
            if (_dataArr[indexPath.row][@"id"] == selArr[i]) {
                
                cell.isSelected=NO;//将cell的颜色设置为未选中的颜色
                [selArr removeObjectAtIndex:i];//从数组中删除选中的话题
                NSLog(@"_choise--%@", selArr);
                return;
            }
        }
    }
    [selArr addObject:_dataArr[indexPath.row][@"id"]];//添加到数组中
    
   cell.isSelected=YES;//设置选择状态的颜色

    NSMutableArray *selIndexArr = [NSMutableArray array];
    for (int i=0; i<selArr.count; i++) {
        selCount=selIndexArr.count ;
        [self.categoryView reloadData];
        NSNumber *s = selArr[i];
        if ([s boolValue]) {
            [selIndexArr addObject:@(i)];
        }
    }
    
    BOOL sel = [selArr[indexPath.item] boolValue];
    [selArr removeObjectAtIndex:indexPath.item];
    [selArr insertObject:@(!sel) atIndex:indexPath.item];
    
    [self.categoryView reloadData];

}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
        return CGSizeMake((KSCREENWIDTH - 88 - 32) / 3, (KSCREENWIDTH - 88 - 32) / 3 + 30);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 0, 0 ,0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 16;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)nextStepClick:(UIButton *)btn{
    @KWeakObj(self)
    NSMutableArray *selIndexArr = [NSMutableArray array];
    for (int i=0; i<selArr.count; i++) {
        NSNumber *s = selArr[i];
        if ([s boolValue]) {
            [selIndexArr addObject:@(i)];
        }
    }
    NSMutableArray *infoArr = [NSMutableArray array];
    for (NSNumber *index in selIndexArr) {
        info = _dataArr[[index integerValue]];
        [infoArr addObject:info.cid];
    }
    NSString * c_idStr = [infoArr componentsJoinedByString:@","];
    iGSetCategoryApi *setApi=[[iGSetCategoryApi alloc]initWithids:c_idStr];
    [setApi startWithDecodeClass:[iGSetCategoryResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            [selfWeak presentMessageTips:NSLocalizedString(@"Sent Successfully", nil)];
        }
    }];
    
}

@end
